import {
  systemType,
  changeUrl,
  TZT_NAVIGATOR,
  isInApp,
  shareInApp,
  tztGetUserCenterInfo,
  tztCloseWebview,
  getLoginData,
  getLoginInfo,
} from "@ret-utils/bridge";
import { RouteLocationNormalizedLoaded, LocationQuery, LocationQueryValue } from "vue-router";
import { decryptUrl } from "./json";
import { storage } from "./storage";

// 意见反馈
export const openFeedback = () => {
  changeUrl("http://action:58122/?");
};

// 在线咨询
export const openOnlineConsultation = () => {
  changeUrl(
    "http://action:10061/?fullscreen=1&&firsttype=10&&secondtype=9&&url=https%3A%2F%2Fzxkf.nesc.cn%3A8092%2Fkwdf_robot%2F%23%2Fchat%3Fbrower%3D0%26model%3D0%26yybqd%3D1303%26devicetype%3Dios%26userid%3D"
  );
};

// 人工客服打电话-临时20230607
export const openManualCustomerService = () => {
  if (systemType() == "1") {
    // ios
    changeUrl("tel:95360/?");
  } else {
    changeUrl("http://tel:95360/?");
  }
};

/**
 * 跳转手机号登录页
 * @param url
 * url 有值则跳转目标页面
 * 无值则返回当前页
 */
export const navigateToWithMobile = (url?: string) => {
  if (url && url !== "") {
    changeUrl("http://action:10301/?url=" + encodeURIComponent(url));
  } else {
    changeUrl("http://action:10301/?");
  }
};

/**
 * 跳转资金账号登录页
 * @param url
 * url 有值则跳转目标页面
 * 无值则返回当前页
 */

export const navigateToWithAccount = (url?: string) => {
  if (isInApp()) {
    if (url && url !== "") {
      changeUrl("http://action:10090/?url=" + encodeURIComponent(url));
    } else {
      changeUrl("http://action:10090/?");
    }
  }
};

// 获取最新用户登录信息
interface QueryParams {
  // 客户号
  userCode?: string | undefined | LocationQueryValue;
  // 手机号
  mobileCode?: string | undefined | LocationQueryValue;
  // 资金账号
  fundAccount?: string | undefined | LocationQueryValue;
  // 中焯票据
  servticketId?: string | undefined | LocationQueryValue;
}

export const getClientInfo = (currentRoute?: RouteLocationNormalizedLoaded): QueryParams => {
  const clientInfo: QueryParams = {};
  if (isInApp()) {
    const userCode = JSON.parse(
      getLoginData({ UserCode: "", FundAccount: "", MobileCode: "" })
    ).UserCode;
    const data = tztGetUserCenterInfo("mobilecode,tztfundaccount");
    const { mobilecode: mobileCode, tztfundaccount: fundAccount } = JSON.parse(data);

    clientInfo.userCode = userCode;
    clientInfo.mobileCode = mobileCode;
    clientInfo.fundAccount = fundAccount;
  } else if (currentRoute?.query?.enData) {
    // 仅限小程序对接使用
    const { userCode, mobileCode, fundAccount } = decryptUrl(currentRoute?.query?.enData as string);

    clientInfo.userCode = userCode;
    clientInfo.mobileCode = mobileCode;
    clientInfo.fundAccount = fundAccount;
  } else if (!import.meta.env.PROD) {
    // 仅限非生产环境开发调试使用
    clientInfo.userCode = storage("userCode");
    clientInfo.mobileCode = storage("mobileCode");
    clientInfo.fundAccount = storage("fundAccount");
  }
  return clientInfo;
};

// 退出资金账号登录
export const exitWithAccount = () => {
  changeUrl("http://action:10402/?");
};

// 重新登录手机号,会退出手机号并唤起手机号登录
export const exitWithMobile = () => {
  changeUrl("http://action:10314/?");
};

// 10061打开新的 webview 页面
export const openWebview = (path: string) => {
  if (isInApp()) {
    // const couponUrl = `${origin}${path}`;
    const url =
      `http://action:10061/?fullscreen=1&&firsttype=10&&secondtype=9&&url=` +
      encodeURIComponent(path);
    changeUrl(url);
  }
};

// 关闭页面
export const closePage = () => {
  if (isInApp()) {
    setTimeout(() => {
      tztCloseWebview();
    }, 1000);
  }
};

/**
 * 获取登录信息,需要资金账号登录后使用
 * 未登录时调用 getLoginInfo() 返回""，使用JSON.parse(getLoginInfo())会报错 Unexpected end of JSON input
at Object.parse 
 * @param currentRoute 
 * @returns 
 * 
 */
export const getLoginInfoByH5 = (currentRoute?: RouteLocationNormalizedLoaded): QueryParams => {
  const loginInfo: QueryParams = {};
  if (isInApp()) {
    const servticketId = JSON.parse(getLoginInfo()).servticket_id;
    loginInfo.servticketId = servticketId;
  } else if (currentRoute?.query?.enData) {
    // 仅限小程序对接使用
    const { servticketId } = decryptUrl(currentRoute?.query?.enData as string);
    loginInfo.servticketId = servticketId;
  } else if (!import.meta.env.PROD) {
    // 仅限非生产环境开发调试使用
    loginInfo.servticketId = storage("servticketId");
  }
  return loginInfo;
};

// 链接分享方式
export const shareNewsUrl = (url?: string, title?: string, message?: string) => {
  // app方法模拟
  if (isInApp()) {
    shareInApp({
      sharetype: "99",
      title,
      message,
      url: url ? url : window.location.href.split("?")[0],
    });
  }
};
